package com.estar.assistsurvy.utils;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class getLocUtil {

	private Context context;
	private GeoCoder mSearch = GeoCoder.newInstance();  
	private MyGetLocListener listener=new MyGetLocListener();
    private Handler handler;
    private String locName;
    
	public getLocUtil(float lat, float lon, Context context) {
		super();
		this.context = context;
		
		mSearch.setOnGetGeoCodeResultListener(listener);  
	    LatLng ptCenter = new LatLng(lat,lon );  
	    // 反Geo搜索  
	    mSearch.reverseGeoCode(new ReverseGeoCodeOption()  
	            .location(ptCenter));
	}
	
	public String getLocName(){
		
		handler=new Handler(){
	    	@Override
	    	public void handleMessage(Message msg) {
	    		// TODO Auto-generated method stub
	    		locName=(String) msg.obj;
	    		
	    	}
	    };
	    
		return locName;
	}


	private class MyGetLocListener implements OnGetGeoCoderResultListener{

		@Override
		public void onGetGeoCodeResult(GeoCodeResult arg0) {
			// TODO Auto-generated method stub
			//这个方法是用来将地名转换为经纬度的
		}

		@Override
		public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
			// TODO Auto-generated method stub
			//这个方法是用来将经纬度改为地名的
			if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  
	            Toast.makeText(context, "抱歉，未能找到结果", Toast.LENGTH_LONG)  
	                    .show();  
	            return;  
	        }  
	        //定位  
	        Toast.makeText(context, result.getAddress(),  
	                Toast.LENGTH_LONG).show();  
	        //result保存翻地理编码的结果 坐标-->城市  
	        Message msg=handler.obtainMessage();
	        msg.obj=result.getAddress();
	        handler.sendEmptyMessage(0);
		}
		
	}

}
